package com.atguigu.srb.core.controller.admin;


import com.atguigu.common.result.R;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author grq
 * @since 2021-04-27
 */
@Api(tags = "积分等级管理")
// @CrossOrigin
@Slf4j
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;

    /**
     * 积分等级列表
     */
    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public R listAll() {
        log.info("info");
        log.error("error");
        log.warn("warn");
        final List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "根据ID删除数据记录", notes = "逻辑删除记录")
    @DeleteMapping("/remove/{id}")
    public R removeById(
            @ApiParam(value = "数据ID", example = "100", required = true)
            @PathVariable Long id) {
        final boolean b = integralGradeService.removeById(id);
        if (b) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "积分等级对象", required = true)
            @RequestBody IntegralGrade integralGrade) {
        final boolean save = integralGradeService.save(integralGrade);
        if (save) {
            return R.ok().message("保存成功");
        }
        return R.error().message("保存失败");
    }

    @ApiOperation("根据ID获取积分等级")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam(value = "数据ID", required = true, example = "1")
                     @PathVariable Long id) {
        final IntegralGrade integralGrade = integralGradeService.getById(id);
        if (integralGrade != null) {
            return R.ok().message("数据获取成功").data("record", integralGrade);
        }
        return R.error().message("数据获取失败");
    }

    @ApiParam(value = "更新积分等级")
    @PutMapping("/update")
    public R update(@RequestBody IntegralGrade integralGrade) {
        final boolean b = integralGradeService.updateById(integralGrade);
        if (b) {
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");
    }


}

